<?php
namespace app\api\controller;
use app\api\controller\Base;
/**
 * 公共信息
 */ 
class Invoice extends Base
{
    /**
     * 获取用户发票信息
     * 
     * 
     */
    public function list()
    {
        $user_id = $this->user_id;
        $list = db('invoice')->where(['user_id'=>$user_id])->order('id desc')->select();
        return model('Res')->ctx($list);
    }
    // 添加发票信息
    public function add()
    {   
        $cate_id = input('post.cate_id');
        $user_id = $this->user_id;
        // 判断是否是个人发票信息
        if(isset($cate_id)){
            if($cate_id==1){
                $name = input('post.name');
                if(!isset($name)){
                    return model('Res')->noParam();
                }
                $list = db('invoice')->insert(['user_id'=>$user_id,'name'=>$name,'cate_id'=>$cate_id]);
            }else{
                $company = input('post.company');
                $indentify_num = input('post.indentify_num');
                $address = input('post.address');
                $phone = input('post.phone');
                $bank = input('post.bank');
                $number = input('post.number');
                if(!isset($indentify_num)){
                    return model('Res')->noParam();
                }
                if(!isset($address)){
                    return model('Res')->noParam();
                }
                if(!isset($phone)){
                    return model('Res')->noParam();
                }
                if(!isset($bank)){
                    return model('Res')->noParam();
                }
                if(!isset($number)){
                    return model('Res')->noParam();
                }
                $data = ['cate_id'=>$cate_id,'company'=>$company,'indentify_num'=>$indentify_num,'address'=>$address,'phone'=>$phone,'bank'=>$bank,'number'=>$number,'user_id'=>$user_id];
                $list = db('invoice')->insert($data);
            }
            if($list){
                return ['code'=>1,'msg'=>'添加成功'];
            }else{
                return ['code'=>0,'msg'=>'添加失败'];
            }
        }else{
            return model('Res')->noParam();
        }
        
        
    }
    public function info()
    {
        $user_id = $this->user_id;
        $id = input('post.id');
        $list = db('invoice')->where(['id'=>$id])->find();
        return model('Res')->ctx($list);
    }
	public function del()
	{
	    $id = input('post.id');
	    if(isset($id)){
	        $list = db('invoice')->delete($id);
	        if($list){
	            return ['code'=>1,'msg'=>'删除成功'];
	        }else{
	            return  ['code'=>0,'msg'=>'删除失败'];
	        }
	    }else{
	        return model('Res')->noParam();
	    }
	}
	public function edit()
	{
	   $cate_id = input('post.cate_id');
        $user_id = $this->user_id;
        // 判断是否是个人发票信息
        if(isset($cate_id)){
            if($cate_id==1){
                $data = input('post.');
                if(!isset($data['name'])){
                    return model('Res')->noParam();
                }
                $data['user_id'] = $user_id;
                 
                $list = db('invoice')->where('id',$data['id'])->update($data);
                if($list){
                    return ['code'=>1,'msg'=>'修改成功'];
                }else{
                    return ['code'=>0,'msg'=>'修改失败'];
                }
                    
            }else{
                $company = input('post.company');
                $indentify_num = input('post.indentify_num');
                $address = input('post.address');
                $phone = input('post.phone');
                $bank = input('post.bank');
                $number = input('post.number');
                if(!isset($indentify_num)){
                    return model('Res')->noParam();
                }
                if(!isset($address)){
                    return model('Res')->noParam();
                }
                if(!isset($phone)){
                    return model('Res')->noParam();
                }
                if(!isset($bank)){
                    return model('Res')->noParam();
                }
                if(!isset($number)){
                    return model('Res')->noParam();
                }
                $data = input('post.');
                $data['user_id'] = $user_id;
                $list = db('invoice')->where(['id'=>$data['id']])->update($data);
            }
            if($list){
                return ['code'=>1,'msg'=>'修改成功'];
            }else{
                return ['code'=>0,'msg'=>'修改失败'];
            }
        }else{
            return model('Res')->noParam();
        }
        
	    
	}
	
	
}